#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=60;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
vector<LL>g[maxn];
map<LL,LL>map1[maxn];
map<LL,bool>vis;
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=m;i++){
    LL u,v;cin>>u>>v;
    map1[u][v]=1;
    map1[v][u]=1;
  }
  LL len=0;LL ansid=0;LL txtlen=0;
  LL T;cin>>T;
  for(LL t=1;t<=T;t++){


      LL num;cin>>num;
      vector<LL>v;
      v.push_back(0);
      for(LL i=1;i<=num;i++){
        LL x;cin>>x;
        v.push_back(x);
      }
      bool flag=1;
      LL res=0;

      vis.clear();
      vis[0]=true;
      res++;

      for(LL i=0;i<v.size()-1;i++){
         LL u=v[i];
         LL to=v[i+1];
         if(map1[u].count(to)&&vis[to]==0){
            res++;
            vis[to]=1;
         }
         else if(!map1[u].count(to)){
            break;
         }
      }
      if(flag==1){
        if(res>len){
            len=res;
            ansid=t;
            txtlen=num;
        }
        else if(res==len){
            if(num>txtlen){
                ansid=t;
                txtlen=num;
                len=res;
            }
            else if(txtlen==num){
                ansid=min(ansid,t);
            }
        }
      }
  }

  cout<<ansid<<"\n";
return 0;
}
